# -*- coding:utf-8 -*-
import time
import json
import stixconfig
from stixhandler import stixhandler
from urllib3.exceptions import ConnectionError
from urllib2 import quote as urlencode, unquote as urldecode


class QueryHandler:
    def __init__(self, body, index=None, doc_type=None):
        try:
            Hstix = stixhandler(body=body, index=index, doc_type=doc_type)
            # self.xmlresult = Hstix.exportxml()
            json_dic = \
            {
                'code':0,
                'msg':'查询成功',
                'body': {'xml':Hstix.exportxml()}
            }
            self.jsonresult = json.dumps(json_dic)

        except ConnectionError, e:
            self.jsonresult = '{"code": 1,"msg":"ES服务器连接超时","body":{}}'

        except Exception, e:
            print e
            self.jsonresult = '{"code": -1,"msg":"请求处理异常","body":{}}'


    def getresult(self):
        return self.jsonresult


